import { getMpMaAppid } from '@/utils/index'
import { login,wxMaLogin, logout, getInfo } from '@/api/authing'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    info: {},
    social: {},
    token: getToken(),
    name: '',
    avatar: '',
    phone: ''
  },

  mutations: {
    SET_USER: (state, info) => {
      state.info = info
    },
    SET_SOCIAL: (state, social) => {
      state.social = social
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      return new Promise((resolve, reject) => {
        login({username: username,password: password,loginType: 2}, { custom: { auth: false }}).then(res => {
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 微信登录
    WxMaLogin({ commit }) {
      return new Promise((resolve, reject) => {
        uni.getUserProfile({desc: '获取您的微信信息以授权小程序',lang: 'zh_CN',
          success: userProfileRes => {
            console.log('----1:',userProfileRes)
            uni.login({provider: 'weixin',success: function(loginRes) {
                console.log('----2:', loginRes);
                wxMaLogin({params: {appid: futureConfig.appid,code: loginRes.code,encryptedData: userProfileRes.encryptedData
                 ,ivStr: userProfileRes.iv}, custom: { auth: false,toast: true }}).then(rseponse => {
                  setToken(rseponse.data.token)
                  commit('SET_TOKEN', rseponse.data.token)
                  resolve()
                }).catch(error => {
                  reject(error)
                })
              }
            })
          },
          fail:err=>{
            console.log(err)
          }
        })
      })
    },
    // 微信手机号一键登录
    WxMaPhoneLogin({ commit }, userInfo) {
      const code = userInfo.code
      return new Promise((resolve, reject) => {
        uni.getUserProfile({desc: '获取您的微信信息以授权小程序',lang: 'zh_CN',
          success: userProfileRes => {
            console.log('----1:',userProfileRes)
            wxMaLogin({params: {appid: futureConfig.appid,maLoginType:'maPhone',code: code,userInfo:userProfileRes.rawData}, custom: { auth: false,toast: true }}).then(rseponse => {
              setToken(rseponse.data.token)
              commit('SET_TOKEN', rseponse.data.token)
              resolve()
            }).catch(error => {
              reject(error)
            })
          },
          fail:err=>{console.log(err)}
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo({params: {}, custom: { auth: true,toast: false }}).then(res => {
          const user = res.data.user
          // console.log('获取用户信息，getInfo: ',user.userName)
          commit('SET_USER', user)
          commit('SET_SOCIAL', res.data.socialUser)
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', '1111111111111')
          commit('SET_PHONE',user.phonenumber)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout({params: {}, custom: { auth: true,toast: false }}).then(() => {
          commit('SET_TOKEN', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
